package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.Announcement;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import retrofit2.Retrofit;

public class AnnouncementViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    private MediatorLiveData<BaseResponse<Page<Announcement>>>announcement;

    private MediatorLiveData<BaseResponse<Announcement>> addAnnouncement;
    private MediatorLiveData<BaseResponse<Announcement>> delAnnouncement;

    public AnnouncementViewModel(Retrofit retrofit) {
        this.mRetrofit = retrofit;
        dcrmApi= mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<Page<Announcement>>>findAnnouncement(int page ,int size){
        announcement=creatLiveData(announcement);
        if(size==0){
            dcrmApi.findAnnouncement(null,size,"createTime_desc")
                    .enqueue(new CallBack<>(this,announcement));
        }else {

            dcrmApi.findAnnouncement(page,size,"createTime_desc")
                    .enqueue(new CallBack<>(this,announcement));
        }
        return announcement;
    }

    public MediatorLiveData<BaseResponse<Page<Announcement>>> getAnnouncement() {
        announcement=creatLiveData(announcement);
        return announcement;
    }

    public MediatorLiveData<BaseResponse<Announcement>> del(long id){
        delAnnouncement=creatLiveData(delAnnouncement);
        dcrmApi.delAnnouncement(id).enqueue(new CallBack<>(this,delAnnouncement));
        return delAnnouncement;
    }
    public MediatorLiveData<BaseResponse<Announcement>> add(String content,String startTime,String endTime){
        addAnnouncement=creatLiveData(addAnnouncement);
        dcrmApi.addAnnouncement(content,startTime,endTime)
                .enqueue(new CallBack<>(this,addAnnouncement));
        return addAnnouncement;
    }

    public MediatorLiveData<BaseResponse<Announcement>> getAddAnnouncement() {
        addAnnouncement=creatLiveData(addAnnouncement);
        return addAnnouncement;
    }

    public MediatorLiveData<BaseResponse<Announcement>> getDelAnnouncement() {

        delAnnouncement=creatLiveData(delAnnouncement);
        return delAnnouncement;
    }
}
